package controller

import (
	"demos/config"
	"demos/model"
	"github.com/gin-gonic/gin"
	"net/http"
	"strconv"
)

// 新建帖子
func AddPost(c *gin.Context) {
	var post model.Post
	if err := c.ShouldBindJSON(&post); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
		return
	}
	username, _ := c.Get("username")
	if usernameStr, ok := username.(string); ok && usernameStr != "" {
		post.Author = usernameStr
	}
	userID, _ := c.Get("userId")
	if id, ok := userID.(uint); ok && id != 0 {
		post.UserID = id
	}
	post.Status = "pending"
	if err := config.DB.Create(&post).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "发帖失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "data": post})
}

// 获取帖子列表（分页、搜索、筛选）
func GetPostList(c *gin.Context) {
	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	pageSize, _ := strconv.Atoi(c.DefaultQuery("pageSize", "10"))
	search := c.Query("search")
	status := c.Query("status")

	var posts []model.Post
	db := config.DB
	if search != "" {
		db = db.Where("title LIKE ? OR author LIKE ?", "%"+search+"%", "%"+search+"%")
	}
	if status != "" {
		db = db.Where("status = ?", status)
	}
	var total int64
	db.Model(&model.Post{}).Count(&total)
	db = db.Order("created_at desc").Offset((page-1)*pageSize).Limit(pageSize)
	db.Find(&posts)
	c.JSON(http.StatusOK, gin.H{
		"code": 0,
		"data": posts,
		"total": total,
		"page": page,
		"pageSize": pageSize,
	})
}

// 删除帖子
func DeletePost(c *gin.Context) {
	id := c.Param("id")
	if err := config.DB.Delete(&model.Post{}, id).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "删除失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "删除成功"})
}

// 审核通过
func ApprovePost(c *gin.Context) {
	id := c.Param("id")
	if err := config.DB.Model(&model.Post{}).Where("id = ?", id).Update("status", "approved").Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "操作失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "已通过"})
}

// 审核拒绝
func RejectPost(c *gin.Context) {
	id := c.Param("id")
	if err := config.DB.Model(&model.Post{}).Where("id = ?", id).Update("status", "rejected").Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "操作失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "msg": "已拒绝"})
}

// 更新帖子
func UpdatePost(c *gin.Context) {
	id := c.Param("id")
	var post model.Post
	if err := config.DB.First(&post, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "帖子不存在"})
		return
	}
	var req struct {
		Title   string `json:"title"`
		Content string `json:"content"`
		Cover   string `json:"cover"`
	}
	if err := c.ShouldBindJSON(&req); err != nil {
		c.JSON(http.StatusBadRequest, gin.H{"error": "参数错误"})
		return
	}
	if req.Title != "" {
		post.Title = req.Title
	}
	if req.Content != "" {
		post.Content = req.Content
	}
	post.Cover = req.Cover // 允许为空
	if err := config.DB.Save(&post).Error; err != nil {
		c.JSON(http.StatusInternalServerError, gin.H{"error": "更新失败"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "data": post})
}

// 获取单个帖子详情
func GetPostDetail(c *gin.Context) {
	id := c.Param("id")
	var post model.Post
	if err := config.DB.First(&post, id).Error; err != nil {
		c.JSON(http.StatusNotFound, gin.H{"error": "帖子不存在"})
		return
	}
	c.JSON(http.StatusOK, gin.H{"code": 0, "data": post})
} 